package com.qcby.service.impl;

import com.qcby.dao.MomentsDao;
import com.qcby.entity.*;
import com.qcby.service.MomentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MomentsServiceImpl implements MomentsService {

    @Autowired
    private MomentsDao momentsDao;

    @Override
    public Result<User> showUserInfoByUserId(int userId) {
        return Result.success(momentsDao.showUserInfoByUserId(userId));
    }

    @Override
    public Result<List<UserCheckin>> showAllCheckinsByPage(int pageSize) {
        List<UserCheckin> moments = momentsDao.showAllCheckinsByPage(pageSize);

        for (UserCheckin moment:
             moments) {
            int momentId = moment.getId();
            // 查询当前评论数量
            int reviewNum = momentsDao.findSingleMomentCommentNum(momentId);
            // 更新当前评论数量
            momentsDao.updateSingleMomentCommentNum(momentId, reviewNum);
            //查询当前点赞数量
            int likeNum = momentsDao.findSingleMomentLikeNum(momentId);
            //更新当前点赞数量
            momentsDao.updateSingleMomentLikeNum(momentId, likeNum);

        }

        return Result.success(momentsDao.showAllCheckinsByPage(pageSize));
    }

    @Override
    public Result<List<Review>> getCommentsByCheckinId(int checkinId) {
        List<Review> reviews = momentsDao.getCommentsByCheckinId(checkinId);
        for (Review review :
                reviews) {
            int userId = review.getUserId();
            User userInfo = momentsDao.showUserInfoByUserId(userId);
            review.setUser(userInfo);
//            System.out.println("userInfo = " + userInfo);

            int reviewId = review.getId();
            //查询当前单个评论的点赞数量
            int likeNum = momentsDao.findSingleReviewLikeNum(reviewId);
            review.setLikeCount(likeNum);
            //更新当前单个评论的点赞数量
            momentsDao.updateSingleReviewLikeNum(reviewId, likeNum);
        }
        System.out.println("reviews = " + reviews);
        return Result.success(reviews);
    }

    @Override
    public Result<List<Review>> getChildrenCommentsByParentId(int parentId) {
        List<Review> reviews = momentsDao.getChildrenCommentsByParentId(parentId);
        for (Review review :
                reviews) {
            int userId = review.getUserId();
            User userInfo = momentsDao.showUserInfoByUserId(userId);
            review.setUser(userInfo);
//            System.out.println("userInfo = " + userInfo);
        }
        return Result.success(reviews);
    }

    @Override
    public Result<Integer> addComment(int checkinId, int userId, String content) {

        //查询当前打卡记录的评论数
        int singleReviewCount = momentsDao.findCheckinReviewCount(checkinId);

        //在评论的同时更新评论数
        momentsDao.updateCommentsCount(singleReviewCount, checkinId);

        momentsDao.addComment(checkinId, userId, content);

        return Result.success(singleReviewCount+1);
    }

    @Override
    public Result<Integer> replyComment(int checkinId, int userId, String content, int parentId) {

        //查询当前打卡记录的评论数
        int singleReviewCount = momentsDao.findCheckinReviewCount(checkinId);

        //在评论的同时更新评论数
        momentsDao.updateCommentsCount(singleReviewCount, checkinId);

        momentsDao.replyComment(checkinId, userId, content, parentId);

        return Result.success(singleReviewCount+1);
    }

    @Override
    public Result<List<Shop>> fuzzyQueryShop(String shopName) {
        return Result.success(momentsDao.fuzzyQueryShop(shopName));
    }

    @Override
    public Result<Integer> publishMoment(String context, String imgsUrls, int userId, int shopId) {
        return Result.success(momentsDao.publishMoment(context, imgsUrls, userId, shopId));
    }
}
